package com.priox.sigar.util;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporterParameter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class ExportUtil {

    public static File exportPdfToFile(String jasperFile, Map<String, Object> parameters, List dataList, String nomReport) throws Exception {
        JasperPrint jasperPrint = exportToJasperPrint(jasperFile, parameters, dataList);

        File file = File.createTempFile(nomReport, ".pdf");

        JRPdfExporter jRPdfExporter = new JRPdfExporter();
        jRPdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
        jRPdfExporter.setParameter(JRPdfExporterParameter.OUTPUT_FILE, file);

        jRPdfExporter.exportReport();

        return file;
    }

    public static File exportExcelToFile(String jasperFile, Map<String, Object> parameters, List dataList, String nomReport) throws Exception {
        JasperPrint jasperPrint = exportToJasperPrint(jasperFile, parameters, dataList);
         File file = File.createTempFile(nomReport, ".xls");
        JExcelApiExporter jExcelApiExporter = new JExcelApiExporter();
        jExcelApiExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
        jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
        jExcelApiExporter.setParameter(JExcelApiExporterParameter.CREATE_CUSTOM_PALETTE, Boolean.TRUE);
        jExcelApiExporter.setParameter(JExcelApiExporterParameter.MAXIMUM_ROWS_PER_SHEET, Integer.decode("65000"));
        jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        jExcelApiExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
        jExcelApiExporter.setParameter(JRPdfExporterParameter.OUTPUT_FILE, file);
        jExcelApiExporter.exportReport();
        return file;
    }

    public static File exportDocxToFile(String jasperFile, Map<String, Object> parameters, List dataList, String nomReport) throws Exception {
        JasperPrint jasperPrint = exportToJasperPrint(jasperFile, parameters, dataList);

        File file = File.createTempFile(nomReport, ".docx");

        JRDocxExporter jRDocxExporter = new JRDocxExporter();
        jRDocxExporter.setParameter(JRDocxExporterParameter.JASPER_PRINT, jasperPrint);
        jRDocxExporter.setParameter(JRDocxExporterParameter.OUTPUT_FILE, file);
        jRDocxExporter.exportReport();
        return file;
    }

    private static JasperPrint exportToJasperPrint(String jasperFile, Map<String, Object> parameters, List dataList) throws IOException, JRException {
        InputStream inputStream = new FileInputStream(jasperFile);
        try {
            JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(dataList, true);
            JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, parameters, jrBeanCollectionDataSource);
            return jasperPrint;
        } finally {
            inputStream.close();

        }
    }
}
